/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ManagedBeans;

import Beans.UserBean;
import java.io.IOException;
import java.io.Serializable;
import javax.ejb.EJB;
import javax.servlet.http.HttpServletResponse;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import radio.entities.AccountDetails;
import radio.entities.Accounts;
import radio.sessions.AccountsFacade;
import radio.sessions.CityFacade;

/**
 *
 * @author Kim
 */
public class LoginForm implements Serializable {

    @EJB
    private CityFacade cityFacade;
    @EJB
    private AccountsFacade accountsFacade;

    /** Creates a new instance of LoginForm */
    public LoginForm() {
    }
    private String username;
    private String password;

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void Login() throws IOException {
//        HttpServletRequest rep=(HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();        

        if (username == null && password == null) {
            System.out.println("Loi");
        } else if (!username.equals("") && !password.equals("")) {
            try {
                Accounts acc = accountsFacade.login(this.username, this.password);

                if (acc != null) {

                    if (acc.getTypeId().getTypeName().equalsIgnoreCase("user")) {
                        AccountDetails detail = acc.getAccountDetails();
                        UserBean us = new UserBean(acc.getUid(), acc.getPwd(), null, acc.getAccountDetails().getContactPerson(), detail.getMobile(), detail.getTelephone(), detail.getAddress(), detail.getEmail(), acc.isGender(), true, acc.getAccId(), acc.getTypeId().getTypeId(), detail.getCityId().getCityId());
                        User ub = new User(us, accountsFacade, cityFacade);
                        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("userBean", ub);

                        HttpSession ses = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
                        ses.setAttribute("userId", acc.getAccId());
                        HttpServletResponse res = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
                        res.sendRedirect("./../User/UserPage.xhtml");
                    } else if (acc.getTypeId().getTypeName().equalsIgnoreCase("admin")) {
                        AccountDetails detail = acc.getAccountDetails();
//            UserBean us=new UserBean(acc.getUid(), acc.getPwd(), null, acc.getAccountDetails().getContactPerson(), detail.getMobile(), detail.getTelephone(), detail.getAddress(), detail.getEmail(), acc.isGender(), true, acc.getAccId(), acc.getTypeId().getTypeId(), detail.getCityId().getCityId());                         
//            FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("admin",ad);                                                        
//            
                        HttpSession ses = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
                        ses.setAttribute("adminId", acc.getAccId());
                        HttpServletResponse res = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
                        res.sendRedirect("./../Admin/AdminPage.xhtml");
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        } //}
        else {
            System.out.println("Loi");
        }
//        
//        List<City> lst=cityFacade.findAll();
//        System.out.println(lst);
    }

    public void logout() throws IOException {
        HttpServletRequest req = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        HttpSession ses = req.getSession();
        ses.removeAttribute("userId");
        HttpServletResponse res = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
        res.sendRedirect("./../Main/MainPage.xhtml");
    }
}